﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.Composite.Events;

namespace EQual.MainModule.Documents.Views
{
    public partial class DocumentsGridViewControl : UserControl   , IDocumentGridView 
    {
        public DocumentsGridViewControl()
        {
            InitializeComponent();
       
        }

        void FileIconControl_OnExit(object sender, WpfApplicationExperimental.AttachmentEventArgs e)
        {
      
        }

        private void dataGrid1_RowEditEnding(object sender, Microsoft.Windows.Controls.DataGridRowEditEndingEventArgs e)
        {
            if(ItemAdded != null)
             ItemAdded  (this, new DataEventArgs<DataAccesLayer.Document>(GetActiveDocument()));
        }

        private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(ItemSelected != null && GetActiveDocument ()!= null)
            ItemSelected  (this, new DataEventArgs<DataAccesLayer.Document>(GetActiveDocument()));
        }

        private DataAccesLayer.Document GetActiveDocument()
        {
            return dataGrid1.SelectedItem as DataAccesLayer.Document;
        }

        public event EventHandler<DataEventArgs<DataAccesLayer.Document>> ItemSelected;

        public event EventHandler<DataEventArgs<DataAccesLayer.Document>> ItemDeleted;

        public event EventHandler<DataEventArgs<DataAccesLayer.Document>> ItemAdded;

        public object Model
        {
            get
            {
                return this.DataContext as EQual.MainModule.Documents.Models.IDocumentsPresentationModel;
            }
            set
            {
                this.DataContext = value;
            }
        }



        #region IDocumentGridView Members

        bool _isEditable;

        public bool IsEditable
        {
            get { return _isEditable; }
            set { _isEditable = value; 

                this.dataGrid1.CanUserAddRows = value;
                this.dataGrid1.CanUserDeleteRows = value;
                
            }
        }

        #endregion


        #region IDocumentGridView Members


        public event EventHandler<DataEventArgs<DataAccesLayer.Document>> ItemSaved;

        #endregion
 

        private void FileIconControl_OnExitingDocument(object sender, RoutedEventArgs e)
        {

   ItemSaved(this, new DataEventArgs<DataAccesLayer.Document>(this.GetActiveDocument() as DataAccesLayer.Document));
        }


    }
}
